package com.njuse_czf.kg.web.controller;

import com.njuse_czf.kg.common.R;
import com.njuse_czf.kg.web.request.QuestionQuery;

import com.njuse_czf.kg.web.service.OptimizeAnswerService;
import com.njuse_czf.kg.web.service.QuestionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/question")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @Autowired
    private OptimizeAnswerService optimizeAnswerService;

    private static final Logger log = LoggerFactory.getLogger(QuestionController.class);

    @PostMapping
    public R<String> getAnswer(@RequestBody QuestionQuery questionQuery){
        try{
            String answer = questionService.answer(questionQuery);
            if(!answer.startsWith("Sorry")){
                answer = optimizeAnswerService.optimize(answer);
            }
            return R.success(answer);
        }catch (Exception e){
            log.error(e.getMessage());
            return R.error(e.getMessage());
        }
    }

}
